import socket
import fcntl
import struct
import os

class tool(object):
    def __init__(self):
        print "--TOOL--"
    def getMyEth0(self):
        ifname = ""
        for iface in os.listdir('/sys/class/net'):
            if 'eth0' in iface:
                ifname = iface
                break
        return ifname

    def getMyIp(self, ifname="none"):
        if ifname == "none":
            ifname = self.getMyEth0()
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        return socket.inet_ntoa(fcntl.ioctl(
            s.fileno(),
            0x8915,  # SIOCGIFADDR
            struct.pack('256s', ifname[:15])
        )[20:24])

    def getMyMac(self, ifname="None"):
        if ifname == "None":
            ifname = self.getMyEth0()
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        info = fcntl.ioctl(s.fileno(), 0x8927,struct.pack('256s', ifname[:15]))
        return ':'.join(['%02x'%ord(char) for char in info[18:24]])
   
    def getNetMask(self,ifname="None"):
        if ifname == "None":
            ifname = self.getMyEth0()
        return socket.inet_ntoa(fcntl.ioctl(socket.socket(socket.AF_INET, socket.SOCK_DGRAM),35099, struct.pack('256s', ifname))[20:24])

if __name__ == "__main__":
    tool = tool()
    print tool.getMyIp(),tool.getMyMac(),tool.getNetMask()
